package main

import (
	"context"
	"fmt"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo/options"

	"go.mongodb.org/mongo-driver/mongo"
)

type User struct {
	Id         int    `json:"_id",bson:"_id"`
	OpenId     string `json:"open_id",bson:"open_id"`
	LoginCount int    `json:"login_count",bson:"login_count"`
}

func main() {
	c := context.Background()
	client, err := mongo.Connect(c, options.Client().ApplyURI("mongodb://192.168.182.141:27017/?readPreference=primary&ssl=false"))
	if err != nil {
		panic(err)
	}
	user := User{
		Id:         111,
		OpenId:     "user111",
		LoginCount: 2,
	}
	collection := client.Database("coolcar").Collection("account")
	decoder := bson.Decoder{}
	err = decoder.Decode(user)
	if err != nil {
		panic(err)
	}
	result, err := collection.InsertOne(c, user)
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
}
